<?php
class UserController extends CController {
	public function filters() {
		return array (
			
			'accessControl' 
		);
	}
	public function accessRules() {
		return array (
			
			array (
				
				'allow', 
				'users' => array (
					
					'@' 
				) 
			), 
			array (
				
				'deny',  // deny all users
				'users' => array (
					
					'*' 
				), 
				'message' => '无权访问' 
			) 
		);
	}
	public function actionLists() {
		$admin = new Admin ( );
		$groupList = Groups::model ()->getAll ();
		$criteria = new CDbCriteria ( );
		$dataProvider = new CActiveDataProvider ( 'Admin', array (
			
			'pagination' => array (
				
				'pageSize' => Yii::app ()->params ['postsPerPage'] 
			), 
			'criteria' => array (
				
				'condition' => "state=1 and passport != 'admin'", 
				'with' => array (
					
					'groupName' 
				) 
			) 
		) );
		$pages = $this->render ( "/tpl/default/showPage", array (
			
			"dataProvider" => $dataProvider, 
			"itemView" => '/tpl/default/user/listpage' 
		), true );
		$smarty = Yii::app ()->smarty;
		$smarty->assign ( "pages", $pages );
		$smarty->assign ( "grouplist", $groupList );
		$smarty->assign ( "body", "user/userlist" );
		$smarty->display ( "layouts.html" );
	}
	public function actionUpdate() {
		$admin = new Admin ( );
		$retAll = $admin->getAdminList ();
		$ret = array ();
		if ($retAll) {
			$ret = $retAll;
		}
		$groupList = Groups::model ()->getAll ();
		$id = addslashes ( $_GET ['id'] );
		$model = $this->loadModel ( $id );
		$smarty = Yii::app ()->smarty;
		$smarty->assign ( "ret", $ret );
		$smarty->assign ( "grouplist", $groupList );
		$smarty->assign ( "passport", $model->passport );
		$smarty->assign ( "group", $model->group );
		$smarty->assign ( "tel", $model->phone );
		$smarty->assign ( "email", $model->email );
		$smarty->assign ( "name", $model->name );
		$smarty->assign ( "id", $id );
		$smarty->assign ( "body", "user/updateUser" );
		$smarty->display ( "layouts.html" );
	}
	
	public function actionEdit() {
		$pwd = '';
		$id = addslashes ( $_POST ['id'] );
		$passport = addslashes ( $_POST ['passport'] );
		$group = addslashes ( $_POST ['group'] );
		$name = addslashes ( $_POST ['name'] );
		$tel = addslashes ( $_POST ['tel'] );
		$email = addslashes ( $_POST ['email'] );
		$admin = new Admin ( );
		if (! empty ( $_POST ['pwd'] )) {
			
			$admin->updateByPk ( $id, array (
				
				"pwd" => md5 ( addslashes ( $_POST ['pwd'] ) ) 
			) );
		}
		$admin->updateByPk ( $id, array (
			
			"passport" => $passport, 
			"group" => $group, 
			"name" => $name, 
			"phone" => $tel, 
			"email" => $email 
		) );
		echo CommonFunc::AjaxCallbackMessage ( 'success' );
		exit ();
	}
	public function actionAdd() {
		$admin = new Admin ( );
		if (isset ( $_POST ) && ! empty ( $_POST )) {
			$admin->passport = addslashes ( $_POST ['passport'] );
			$admin->pwd = addslashes ( $_POST ['pwd'] );
			$admin->group = addslashes ( $_POST ['group'] );
			$admin->phone = addslashes ( $_POST ['tel'] );
			$admin->email = addslashes ( $_POST ['email'] );
			$admin->name = addslashes ( $_POST ['name'] );
			if ($admin->save ()) {
				echo CommonFunc::AjaxCallbackMessage ( 'success', 200, $admin->id );
				exit ();
			}
		}
		$this->actionLists ();
	}
	public function actionDel() {
		$admin = new Admin ( );
		$data = addslashes ( $_POST ['data'] );
		$ids = explode ( ";", $data );
		foreach ( $ids as $id ) {
			if ($id) {
				$groups = new Groups ( );
				$admin->updateByPk ( $id, array (
					
					'state' => 0 
				) );
			}
		}
		echo CommonFunc::AjaxCallbackMessage ( 'success' );
		exit ();
	
	}
}
?>